package com.example.testrss;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class IotdHandler extends DefaultHandler {

	// Test for Git
	private static String GIT2_2 = "my second test git 2";
	
	private static String LOG_TAG = "Test_RSS";
	
	private String url = "http://www.nasa.gov/rss/dyn/image_of_the_day.rss";
	private boolean inUrl = false;
	private boolean inTitle = false;
	private boolean inDescription = false;
	private boolean inItem = false;
	private boolean inDate = false;
	private Bitmap image = null;
	private String title = null;
	private StringBuffer description = new StringBuffer();
	private String date = null;
	private String imageUrl = null;

	public void proceedFeed() {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			reader.setContentHandler(this);

			Log.i(LOG_TAG, "proceedFeed() ... 1");
			
			InputStream inputStream = new URL(url).openStream();

			reader.parse(new InputSource(inputStream));
			
		} catch (ParserConfigurationException e) {
			Log.e(LOG_TAG, "ParserConfigurationException");
		} catch (SAXException e) {
			Log.e(LOG_TAG, "SAXException");
		} catch (MalformedURLException e) {
			Log.e(LOG_TAG, "MalformedURLException");
		} catch (IOException e) {
			Log.e(LOG_TAG, "IOException");
		} // try-catch

	} // proceedFeed()

	private Bitmap getBitmap(String url) {

		Bitmap bitmap = null;
		try {
			HttpURLConnection connection = 
					(HttpURLConnection) new URL(url).openConnection();
			connection.setDoInput(true);
			connection.connect();

			InputStream input = connection.getInputStream();
			bitmap = BitmapFactory.decodeStream(input);
			input.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} // try-catch

		return bitmap;
	} // getBitmap()

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		Log.i(LOG_TAG, ">>> startElement() >>>");
//		Log.i(LOG_TAG, "uri: " + uri);
//		Log.i(LOG_TAG, "localName: " + localName);
//		Log.i(LOG_TAG, "qName: " + qName);
//		Log.i(LOG_TAG, "attributes: " + attributes.toString());
		
//		if(localName.equals("link")) {
//			inUrl = true;
//		} else {
//			inUrl = false;
//		} // if-else
		
		if(localName.startsWith("item")) {
			inItem = true;
		} else if(inItem) {
			if(localName.equals("title")) {
				inTitle = true;
			} else {
				inTitle = false;
			}
			
			if(localName.equals("enclosure")) {
				Log.i(LOG_TAG, "enclosure???");
				inUrl = true;
			} else {
				inUrl = false;
			}
			
			if(localName.equals("description")) {
				inDescription = true;
			} else {
				inDescription = false;
			}
			
			if(localName.equals("pubDate")) {
				inDate = true;
			} else {
				inDate = false;
			}
		} // if-else
		
	} // startElement()
	
	public void characters(char ch[], int start, int length) {
		
		Log.i(LOG_TAG, ">>> characters() >>>");
//		Log.i(LOG_TAG, "ch: " + ch);
//		Log.i(LOG_TAG, "start: " + start);
//		Log.i(LOG_TAG, "length: " + length);
		
		String chars = new String(ch).substring(start, start + length);
//		Log.i(LOG_TAG, "chars: " + chars);
		
		if(inUrl && imageUrl == null) {
			Log.i(LOG_TAG, "inUrl:chars = " + chars);
			image = getBitmap(chars);
		}
		
		if(inTitle && title == null) {
			Log.i(LOG_TAG, "inTitle:chars = " + chars);
			title = chars;
		}
		
		if(inDescription) {
			Log.i(LOG_TAG, "inDescription:chars = " + chars);
			description.append(chars);
		}
		
		if(inDate && date == null) {
			Log.i(LOG_TAG, "inDate:chars = " + chars);
			date = chars;
		}
		
	} // characters()

	public Bitmap getImage() {
		return image;
	}

	public String getTitle() {
		return title;
	}

	public StringBuffer getDescription() {
		return description;
	}

	public String getDate() {
		return date;
	}

} // class IotdHandler
